﻿using Forum.Entity.CodeFirst.model;
using SqlSugar;

namespace Forum.Entity.CodeFirst
{
    public class AppDbContext
    {
        public SqlSugarClient Db;

        public AppDbContext()
        {
            Db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = "Data Source=180.76.128.139;port=3306;Initial Catalog=Forum;Persist Security Info=True;User ID=curry;Password=curryP@ssw0rd;",
                DbType = DbType.MySql,//设置数据库类型
                IsAutoCloseConnection = true,//自动释放数据库，如果存在事务，在事务结束之后释放。
                InitKeyType = InitKeyType.Attribute//从实体特性中读取主键自增列信息   
            });


            Db.Aop.OnLogExecuting = (sql, pars) =>
            {
                Console.WriteLine(sql + "\r\n" + Db.Utilities.SerializeObject
                    (pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                Console.WriteLine();
            };
        }

        public void CreateTable(bool Backup = false, int StringDefaultLength = 50, params Type[] types)
        {
            Db.CodeFirst.SetStringDefaultLength(StringDefaultLength);
            Db.DbMaintenance.CreateDatabase();
            if (Backup)
            {
                Db.CodeFirst.BackupTable().InitTables(types);
            }
            else
            {
                Db.CodeFirst.InitTables(types);
            }
        }

        //public SimpleClient<Category> categoryDb { get { return new SimpleClient<Category>(Db); } }
        //public SimpleClient<ConfigItem> ConfigItemDb { get { return new SimpleClient<ConfigItem>(Db); } }
        //public SimpleClient<FeaturedCategory> FeaturedCategoryDb { get { return new SimpleClient<FeaturedCategory>(Db); } }
        //public SimpleClient<FeaturedPhoto> FeaturedPhotoDb { get { return new SimpleClient<FeaturedPhoto>(Db); } }
        //public SimpleClient<FeaturedPost> FeaturedPostDb { get { return new SimpleClient<FeaturedPost>(Db); } }
        //public SimpleClient<Link> LinkDb { get { return new SimpleClient<Link>(Db); } }
        //public SimpleClient<LinkExchange> LinkExchangeDb { get { return new SimpleClient<LinkExchange>(Db); } }
        //public SimpleClient<Photo> PhotoDb { get { return new SimpleClient<Photo>(Db); } }
        //public SimpleClient<Post> postDb { get { return new SimpleClient<Post>(Db); } }
        //public SimpleClient<TopPost> TopPostDb { get { return new SimpleClient<TopPost>(Db); } }
        //public SimpleClient<VisitRecord> VisitRecordDb { get { return new SimpleClient<VisitRecord>(Db); } }


    }

}
